/**
* 创建者：lym
* 该页面用于：
* 创建时间： 2020-01-02 20:25:57
*/
import Map from "@/components/ly_components/map/Map"

import { getGIS } from "@/api/RCIM/RCIM-geographic"
import { getRightAndCount } from "@/api/RCIM/RCIM-information"
import { cloneDeep } from "lodash";
export default {
    name: 'Geographic_view',
    components: {
        Map
    },
    // 数据
    data() {
        return {
            search: "",
            center: {lng: 105.0, lat: 38.0 },
            markerPoints: [],
            countData: [{name: '源总数',count: 0},{name: '企业总数',count: 0},{name: '人员总数',count: 0}],
            cloneCountData: [],
            cloneMarkerPoints: [],
            hasMyMarkerClusterer: false,
            showSourceCheck: true,
            myCircleData: [], // 圈的数据，只有固定源和车有数据
            deepCloneMyCircleDate: []
        }
    },
    // 监听
    watch: {

    },
    // Vue实例初始化完成
    created() {
        // this.getRightAndCount()
    },
    // Vue挂载完成
    mounted() {

    },
    // Vue实例销毁前
    beforeDestroy(){

    },
    // 挂载的方法
    methods: {
        /**
		 * 获取源总数
		 */
		async getRightAndCount() {
			const {
				data
            } = await getRightAndCount()
            this.countData = []
            data.countData.forEach(item => {
                if(item.name != "车辆"){
                    this.countData.push(item)
                }
            })
            this.cloneCountData = cloneDeep(this.countData)
		},
        async handleChangeMapData(params) {
            const tempCircleData = []
            let checkListGroup
            if(params.checkListGroup){
                checkListGroup = params.checkListGroup
                delete params.checkListGroup
            }
            this.hasMyMarkerClusterer = true
            this.countData = [{name: '源总数',count: 0},{name: '企业总数',count: 0},{name: '人员总数',count: 0}]
            const mapData = await getGIS(params)
            if (!mapData.data.length) return
			this.center = mapData.data[0].point;
            const markerPoints = mapData.data.map(item => {
                item.infoWindowShow = false
                switch (item.infoWindowType) {
                    case "enterprise" :
                        item.infoWindowInfo.enterpriseInfo.enterpriseImgUrl=item.infoWindowInfo.enterpriseInfo.enterpriseImgUrl || require('../../assets/info_images/noPicture.png')
                        item.markerIcon = {url: 'static_img/map/enterprise.png', size: {width: 24, height: 24}}
                        item.infoWindowWidth = 0
                        item.infoWindowHeight = 140
                        item.offset = {width: 70, height: 0}
                        this.countData[1].count++
                        break
                    case "enterpriseOrigin" :
                        item.markerIcon = {url: require('../../assets/info_images/ora_source.png'), size: {width: 24, height: 24}}
                        item.infoWindowWidth = 0
                        item.infoWindowHeight = 190
                        item.offset = {width: 70, height: 0}
                        this.countData[0].count++

                        // item.infoWindowInfo.enterpriseOriginInfo.enterpriseOriginAddress = ""
                        // item.infoWindowInfo.enterpriseOriginInfo.enterpriseOriginTel = ""
                        // item.infoWindowInfo.enterpriseOriginInfo.enterpriseOriginCity = ""

                        const cicleData = {}
                        cicleData.infoWindowType = 'enterpriseOrigin'
                        cicleData.center = item.point
                        cicleData.radius = item.radius?item.radius:0
                        tempCircleData.push(cicleData)
                        break
                    case "car" :
                        item.markerIcon = {url: require('../../assets/info_images/car.png'), size: {width: 24, height: 24}}
                        item.infoWindowWidth = 280
                        item.infoWindowHeight = 120
                        item.offset = {width: 70, height: 0}

                        const cicleData1 = {}
                        cicleData.infoWindowType = 'car'
                        cicleData1.center = item.point
                        cicleData1.radius = item.radius?item.radius:0
                        tempCircleData.push(cicleData1)
                        break
                    case "person" :
                        item.markerIcon = {url: require('../../assets/info_images/person.png'), size: {width: 24, height: 24}}
                        item.infoWindowWidth = 0
                        item.infoWindowHeight = 80
                        item.offset = {width: 70, height: 0}
                        this.countData[2].count++
                        break
                    case "origin" :
                        item.markerIcon = {url: require('../../assets/info_images/source.png'), size: {width: 24, height: 24}}
                        item.infoWindowWidth = 0
                        item.infoWindowHeight = 140
                        item.offset = {width: 70, height: 0}
                        this.countData[0].count++
                        break
                    case "site" :
                        item.markerIcon = {url: require('../../assets/info_images/ora_source.png'), size: {width: 24, height: 24}}
                        item.infoWindowWidth = 0
                        item.infoWindowHeight = 100
                        item.offset = {width: 70, height: 0}
                        // this.countData[1].count++
                        break
                    default:
                        item.markerIcon = {url: require('../../assets/info_images/source.png'), size: {width: 24, height: 24}}
                        break
                }
                return item
            })
            this.countData = cloneDeep(this.countData)
            this.cloneCountData = cloneDeep(this.countData)
            this.cloneMarkerPoints = cloneDeep(markerPoints)
            // this.hasMyMarkerClusterer = false;
            this.markerPoints = markerPoints;
            // this.changeMiddleLabel(this.markerPoints)
            if(this.markerPoints.length){
                // this.hasMyMarkerClusterer = true;
            }
            this.myCircleData = cloneDeep(tempCircleData)
            this.deepCloneMyCircleDate = cloneDeep(tempCircleData)

            this.handleChangeCheckGroup(cloneDeep(MAP_DEFAULT_SELECT_PLY))
            if(checkListGroup) {
                this.handleChangeCheckGroup(cloneDeep(checkListGroup))
            }
        },
        handleChangeCheckGroup(params){
            this.hasMyMarkerClusterer = true;
            console.log(params)
            const rules = this.getRolues(params)
            console.log(rules)
            // if(rules.infoWindowType.length){
                this.markerPoints = this.cloneMarkerPoints.filter(item => {
                    console.log(item.infoWindowType, rules.infoWindowType.includes(item.infoWindowType))
                    let isInfoWindowType = rules.infoWindowType.includes(item.infoWindowType)
                    let isShoworigin = true;
                    let isShowOriginStatus = true
                    if(isInfoWindowType && (item.infoWindowType === "origin" || item.infoWindowType === "enterpriseOrigin") && rules.originTypeId){
                        let valueOfObj = item?.infoWindowInfo?.originInfo || item?.infoWindowInfo?.enterpriseOriginInfo || {};
                        let valueOfData = valueOfObj.originTypeId ?? '';
                        isShoworigin = rules.originTypeId.includes(valueOfData)

                        let originStatus = valueOfObj.originStatus ?? '';
                        isShowOriginStatus = rules.originStatus.includes(originStatus)
                    }
                    return isInfoWindowType && isShoworigin && isShowOriginStatus
                })
            // } else {
            //     console.log("2222")
            //     this.markerPoints = cloneDeep(this.cloneMarkerPoints)
            // }
            // console.log(this.cloneMarkerPoints)
            // console.log(this.deepCloneMyCircleDate)
            this.myCircleData = this.deepCloneMyCircleDate.filter(item => {
                console.log(item.infoWindowType, rules.infoWindowType.includes(item.infoWindowType))
                let isInfoWindowType = rules.infoWindowType.includes(item.infoWindowType)
                let isShoworigin = true;
                let isShowOriginStatus = true
                // if(isInfoWindowType && (item.infoWindowType === "origin" || item.infoWindowType === "enterpriseOrigin") && rules.originTypeId){
                //     let valueOfObj = item?.infoWindowInfo?.originInfo || item?.infoWindowInfo?.enterpriseOriginInfo || {};
                //     let valueOfData = valueOfObj.originTypeId ?? '';
                //     isShoworigin = rules.originTypeId.includes(valueOfData)

                //     let originStatus = valueOfObj.originStatus ?? '';
                //     isShowOriginStatus = rules.originStatus.includes(originStatus)
                // }
                return isInfoWindowType && isShoworigin && isShowOriginStatus
            })

            this.changeMiddleLabel(this.markerPoints) // 处理中间源总数，人员，的显示
            console.log(this.markerPoints)
            if(this.markerPoints.length){
                // this.hasMyMarkerClusterer = true;
            }
            // this.$forceUpdate()
        },
        getRolues(params){
            const result = {

            }
            result.infoWindowType = []
            params.checkList4.forEach(item => {
                result.infoWindowType.push(...item.split('__'))
            })
            if(params.checkList4.includes('origin__enterpriseOrigin')){
                this.showSourceCheck = true;
            }else{
                this.showSourceCheck = false;
                return result
            }
            result.originTypeId = [];
            params.checkList3.forEach(item => {
                result.originTypeId.push(item)
            })
            // 处理checkList2的过滤
            result.originStatus = [];
            params.checkList2.forEach(item => {
                result.originStatus.push(item)
            })

            const needDel = ['origin', 'enterpriseOrigin']
            let i = 0;
            while(needDel[i]){
                if(params.checkList1.includes(needDel[i])){
                    needDel.splice(i, 1)
                }else{
                    i++;
                }
            }
            needDel.forEach((item) => {
                let idex = result.infoWindowType.findIndex(itemType => itemType === item)
                result.infoWindowType.splice(idex, 1)
            })
            console.log('需要删除的key', needDel)
            return result
        },
        changeMiddleLabel() {
            this.countData = [];
            const mun = {
                "源总数": 0,
                "企业总数": 0,
                "人员总数": 0,
            }
            this.markerPoints.forEach(item => {
                if(item.infoWindowType === "origin" || item.infoWindowType === "enterpriseOrigin"){
                    mun["源总数"] ++;
                }
                if(item.infoWindowType === "enterprise"){
                    mun["企业总数"] ++;
                }
                if(item.infoWindowType === "person"){
                    mun["源总数"] ++;
                }
            })
            this.cloneCountData.forEach(item1 => {
                switch (item1.name) {
                    case "源总数": {
                        this.countData.push(Object.assign({}, item1, {count: mun[item1.name]}))
                        break
                    }
                    case "企业总数": {
                        this.countData.push(Object.assign({}, item1, {count: mun[item1.name]}))
                        break
                    }
                    case "人员总数": {
                        this.countData.push(Object.assign({}, item1, {count: mun[item1.name]}))
                        break
                    }
                }
            })
        },
        changeCenterPoint(point) {
            this.center = point
        }
    }
}
